<?php
/**
 * 农名类
 * User: 16
 * Date: 2018/9/19
 * Time: 9:03
 */
namespace state;

class Farmer{
    /**
     * 当前季节
     * @var
     */
    private $_currentSeason;

    /**
     * 一年四季
     * @var array
     */
    private $_season = [
        'spring',
        'summer',
        'autumn',
        'winter'
    ];

    /**
     * 当前耕作状态
     * @var
     */
    private $_state;

    /**
     * 设置耕作状态
     * @desc
     * @param $currentSeason
     * @author 16
     * @date 2018/9/19
     */
    private function setState($currentSeason){
        if(in_array($currentSeason,$this->_season)){
            $farm = "state\Farm".ucwords($currentSeason);
            $this->_state = new $farm();
        }else{
            throw \Exception("not found season");
        }
    }

    private function nextSeason(){
        $nowKey = (int)array_search($this->_currentSeason,$this->_season);
        $nextSeason = $nowKey < 3 ? $this->_season[$nowKey+1] : $nextSeason = 'spring';
        $this->_currentSeason = $nextSeason;
        $this->setState($this->_currentSeason);
    }

    /**
     * 初始化状态
     * Farmer constructor.
     */
    public function __construct($season="spring")
    {
        $this->_currentSeason = $season;
        $this->setState($this->_currentSeason);
    }

    /**
     * 种植
     * @desc
     * @author 16
     * @date 2018/9/19
     */
    public function grow(){
        $this->_state->grow();
    }

    /**
     * 种植
     * @desc
     * @author 16
     * @date 2018/9/19
     */
    public function harvest(){
        $this->_state->narvest();
        $this->nextSeason();
    }
}